package io.mycat.encrypt.factory;

import io.mycat.encrypt.EncryptAutoHandler;
import io.mycat.encrypt.constant.DbType;
import io.mycat.encrypt.impl.MysqlEncryptAutoHandler;
import io.mycat.encrypt.impl.OracleEncryptAutoHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName EncryptAutoFactory
 * @Author zrx
 * @Date 2022/8/26 10:13
 */
public class EncryptAutoFactory {

	private static final Map<String, String> DATABASE_MAPPER = new HashMap<String, String>() {
		{
			//目前支持mysql和oracle
			put(DbType.MYSQL.name().toLowerCase(), MysqlEncryptAutoHandler.class.getName());
			put(DbType.ORACLE.name().toLowerCase(), OracleEncryptAutoHandler.class.getName());
		}
	};

	public static EncryptAutoHandler getEncryptAutoHandler(String dbType) {
		if (DATABASE_MAPPER.containsKey(dbType)) {
			String className = DATABASE_MAPPER.get(dbType);
			try {
				return (EncryptAutoHandler) Class.forName(className).newInstance();
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		throw new RuntimeException("不支持自动加密的数据库类型！");

	}
}
